package com.dw.etl.core.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.model.SysRoles;
import com.dw.etl.core.bean.model.SysTag;
import com.dw.etl.core.api.CoreAuthorizationService;
import com.dw.etl.core.api.RolesService;
import com.dw.etl.core.api.TagService;
import com.dw.etl.core.utils.Ognlutis;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author GuoPeng
 * @Title: TagController
 * @Description: 标签控制器
 * @date 2019/5/14 19:06
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
public class TagController {


    @Resource
    private TagService tagService;

    @Resource
    private CoreAuthorizationService coreAuthorizationService;


    //为权限添加标签
    @RequestMapping("/RolesSetTag")
    @ResponseBody
    public Msg RolesSetTag(String LoginKey, String data, Integer id){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            Map map=  (Map)gson.fromJson(data, new TypeToken<Map>(){}.getRawType());
            return tagService.PermissionsSetTag(map, id);
        } else {
            return Msg.fail();
        }
    }

    //获取所有标签
    @RequestMapping("/getTagAll")
    @ResponseBody
    public Msg  getRolesAll(String LoginKey,Integer id){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return tagService.SelectAll(id);
        } else {
            return Msg.fail();
        }
    }



    //以下为角色的增删改查
    @RequestMapping("/insertTag")
    @ResponseBody
    public Msg  insertRoles(String LoginKey,String data){
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            SysTag sysTag=  (SysTag)gson.fromJson(data, new TypeToken<SysTag>(){}.getRawType());
            try {
                return tagService.insertTag(sysTag);
            }catch (Exception e){
                return Msg.fail().add("info", "标签名称已存在！");
            }

        } else {
            return Msg.fail();
        }
    }


    @RequestMapping("/getTagList")
    @ResponseBody
    public LayMsg getRoleslist(String LoginKey, Integer page, Integer limit) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            return tagService.getTagList(page, limit);
        } else {
            return LayMsg.fail();
        }
    }

    @ResponseBody
    @RequestMapping("/updateTag")
    public Msg updateRoles(String LoginKey, String tag) {
        Gson gson = new Gson();
        SysTag  sysTag = (SysTag) gson.fromJson(tag, new TypeToken<SysTag>() {
        }.getRawType());
        Msg msg = coreAuthorizationService.autLoginToken(LoginKey);
        if (msg.getCode() == 100) {
            return tagService.updateTag(sysTag);
        } else {
            return msg;
        }
    }


    @RequestMapping("/delectTag")
    @ResponseBody
    public Msg delectRoles(String LoginKey, String tag) {
        if (coreAuthorizationService.autLoginToken_sta(LoginKey)) {
            Gson gson = new Gson();
            List<Map> maps = (List) gson.fromJson(tag, new TypeToken<List<Map>>() {
            }.getRawType());
            for (int i = 0; i < maps.size(); i++) {
                Integer id = Ognlutis.getNumber("id", maps.get(i)).intValue();
                Msg delectuser = tagService.delectTag(id);
            }
            return Msg.success();
        }
        return Msg.fail();
    }

}
